﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataManager;
using StoreClientModule;
namespace MBB_Store_Sales_Report
{
    public partial class frmExpensesEntry : Form
    {
        ExpensesRecords er = new ExpensesRecords();
        DataTable RecordTable = new DataTable();
        string fID = "";

        public frmExpensesEntry()
        {
            InitializeComponent();

            btnAdd.Click += new EventHandler(btnAdd_Click);
            
           
            btnSave.Click += new EventHandler(btnSave_Click);
            btnCommit.Click += new EventHandler(btnCommit_Click);
            txtExpensesAmount.LostFocus += new EventHandler(txtExpensesAmount_LostFocus);
           
            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);

            er.ProcessCompleted += new ExpensesRecords.MyEventHandler(er_ProcessCompleted);
            er.ProcessError += new ExpensesRecords.MyEventHandler(er_ProcessError);
        }

        void txtExpensesAmount_LostFocus(object sender, EventArgs e)
        {
            try
            {
                double num = double.Parse(txtExpensesAmount.Text);
                txtExpensesAmount.Text = Common.FormatCurrency(num);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Invalid Input: " + ex.Message);
            }
        }

        void btnCommit_Click(object sender, EventArgs e)
        {
            if (grd.Rows.Count > 0)
            {
                ExpensesEntryCommit[] eec = new ExpensesEntryCommit[grd.Rows.Count];
                for (int i = 0; i < grd.Rows.Count; i++)
                {
                    eec[i].name = grd.Rows[i].Cells[0].Value.ToString();
                    eec[i].amount = grd.Rows[i].Cells[1].Value.ToString();
                    eec[i].type = grd.Rows[i].Cells[2].Value.ToString();
                    eec[i].date = txtDate.Text;

                    
                }
                er.Commit(eec);
                clearFields();
                MessageBox.Show("Record/s Successfully Saved");
            }
        }

        void er_ProcessError(string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg);
            });
        }
        void er_ProcessCompleted(string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                if (er.ProcessCode == 0)
                {
                    RecordTable = er.RecordTable;
                    grd.DataSource = RecordTable;
                    grd.Columns["Code"].Visible = false;
                    computeTotal();
                }
                if (er.ProcessCode == 1)
                {
                    RecordTable.Rows.Clear();

                }
                if (er.ProcessCode == 2)
                {
                    if (er.Updated == true)
                    {
                        MessageBox.Show("Record Updated");
                    }
                }
            });
        }

       
        void btnSave_Click(object sender, EventArgs e)
        {
            ExpensesEntryCommit expenses = new ExpensesEntryCommit();
            //check input if not null
            if (txtExpensesName.Text != "" && txtExpensesType.Text != "" && txtExpensesAmount.Text != "")
            {
                expenses.id = fID;
                expenses.name = txtExpensesName.Text;
                expenses.amount = txtExpensesAmount.Text;
                expenses.type = txtExpensesType.Text;
                expenses.date = txtDate.Text;

                er.UpdateRecords(expenses);
                computeTotal();
            }
            else
                MessageBox.Show("Please Choose From the List Above");
        }
       
      
        void btnAdd_Click(object sender, EventArgs e)
        {
           
            //check input if not null
            if (txtExpensesName.Text != "" && txtExpensesType.Text != "" && txtExpensesAmount.Text != "")
            {
                DataRow dr = RecordTable.NewRow();
                dr["Expenses Name"] = txtExpensesName.Text;
                dr["Amount"] = txtExpensesAmount.Text;
                dr["Type"] = txtExpensesType.Text;
                
                RecordTable.Rows.Add(dr);

                clearFields();
                computeTotal();
            }
            else
                MessageBox.Show("Please Complete the Required Field.");
        }

        public void buttonChanged01()
        {
         
            btnSave.Visible = true;
            txtDate.Enabled = true;
            btnAdd.Visible = false;
            btnCommit.Visible = false;
        }
        
        void clearFields()
        {
            txtExpensesAmount.Text = "";
            txtExpensesName.Text = "";
            txtExpensesType.Text = "";
            txtTotal.Text = "";
            
        }
        void computeTotal()
        {
            double tAmount = 0.0f;
            for (int i = 0; i < grd.Rows.Count; i++)
            {
                double amt = 0.0f;
                double.TryParse(grd.Rows[i].Cells[1].Value.ToString(), out amt);
                tAmount = tAmount + amt;
            }
            txtTotal.Text = Common.FormatCurrency(tAmount);
        }

        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlExpensesEntry.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlExpensesEntry; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;

            grd.DataSource = RecordTable;
            grd.Font = new Font("Courier New", 8.5f);

            RecordTable.Columns.Add("Expenses Name");
            RecordTable.Columns.Add("Amount");
            RecordTable.Columns.Add("Type");

           
        }

        public void oldData(ExpensesEntryCommit old)
        {
            fID = old.id;
            txtDate.Text = old.date;
            txtExpensesName.Text = old.name;
            txtExpensesAmount.Text = old.amount;
            txtExpensesType.Text = old.type;
        }
    }
}
